//
//  SYStautsCell.swift
//  SYWeibo
//
//  Created by Eric on 16/7/24.
//  Copyright © 2016年 Eric. All rights reserved.
//

import UIKit
import SnapKit
//cell中使用的间距
let statusCellMargin: CGFloat = 10
class SYStautsCell: UITableViewCell {

    //记录底部视图的顶部约束
    var toolBarBottomConstraint: Constraint?
    //定义一个ViewModel
    var statusViewModel: SYStatusViewModel? {
        didSet{
            //MARK: - 判断有无转发微博
            // 给原创微博赋值  解决冲突
            /*
            - 首先要先卸载底部视图的顶部约束
            - 如果retweeted_status == nil 代表没有转发微博
            - 底部视图的top ==  原创微博的bottom
            - 隐藏转发微博
            - 反之 就是有转发微博
            - 给转发微博的ViewModel 赋值
            - 底部视图的top ==  转发微博的bottom
            - 显示转发微博
            */
            
        originalView.statusViewModel = statusViewModel
        
            self.toolBarBottomConstraint?.uninstall()//卸载  01
            //没转发微博
            if statusViewModel?.status?.retweeted_status == nil {
            
            toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                //解决冲突  02
                self.toolBarBottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
            })
            //影藏
                retweetView.hidden = true
                
            }else{
            //记住先赋值  在做约束
                retweetView.statusViewModel = statusViewModel
            //有转发微博的
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                    })
                //显示出来
                retweetView.hidden = false
            }
            // 给底部视图赋值
            toolBarView.statusViewModel = statusViewModel

        }

    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        //设置约束
        //原创微博
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            //MARK :  -加一个灰色条
            //cell的颜色设置成灰色,转载和原创颜色是白色
            
            
            //加一个灰色条**************
            make.top.equalTo(contentView).offset(8)
        }
        
        //转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        //底部toolBar视图
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
            
        }
        
        //当前父view
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
        
    }
    
    
    
    //懒加载控件
    //原创微博
    private lazy var originalView: SYStatusOriginalView = SYStatusOriginalView()
    //转发微博
    private lazy var retweetView: SYStatusRetweetView = SYStatusRetweetView()
    //底部tooTar视图
    private lazy var toolBarView: SYStatusToolBarView = SYStatusToolBarView()
    
}
